package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */

    @Transactional
    public void saveSetMeal(SetmealDTO setmealDTO) {
        log.info("开始新增 套餐啦{}", setmealDTO);
        Setmeal setMeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setMeal);
        //新增套餐 默认为停售状态
        setMeal.setStatus(0);
        setmealMapper.saveSetMeal(setMeal);
        Long id = setMeal.getId();
        //新增套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给每一个套餐菜品设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        setmealDishMapper.saveSetmealDish(setmealDishes);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOS = setmealMapper.queryPage(setmealPageQueryDTO);
//        System.out.println(setmealVOS);
        return new PageResult(setmealVOS.getTotal(), setmealVOS.getResult());
    }

    /**
     * "批量删除套餐"
     *
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //规则 起售中的套餐不可以删除
        ids.forEach(id -> {
            //查询当前选择的套餐是否存在起售的
            Setmeal setmeal = setmealMapper.getSetMealById(id);
            if (setmeal == null || setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //未起售  可以删除
        //先删除套餐 再删除套餐关联的菜品
        ids.forEach(id -> {
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        });
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Transactional
    public void updateSetMealInfo(SetmealDTO setmealDTO) {
        //修改两张表
        //修改stmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //时间都为空
        setmealMapper.update(setmeal);
        //修改stemeal_dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //setmealID null
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.delete(setmealId);
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.saveSetmealDish(setmealDishes);
//        setmealDishes.forEach( setmealDish -> {
//
//            setmealDishMapper.update(setmealDish);
//        });

    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getSetMealById(Long id) {
        //查setmeal表
        Setmeal setmeal = setmealMapper.getSetMealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        //查category表
        if (setmeal != null) {
            Category category = categoryMapper.getCategoryById(setmeal.getCategoryId());
            setmealVO.setCategoryName(category.getName() != null ? category.getName() : null);
        }
        //查setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetMealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 设置套餐状态
     *
     * @param id     套餐id
     * @param status 套餐状态 1 起售 0 停售
     */
    public void setSetMealStatus(Long id, int status) {
        Setmeal setmeal = setmealMapper.getSetMealById(id);
        //判断状态类型
        //套餐下面有停售的菜品不能起售套餐
        if (status == StatusConstant.ENABLE) {
            //获得改套餐下的所有菜品
            List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetMealId(id);
            setmealDishes.forEach(setmealDish -> {
                Long dishId = setmealDish.getDishId();
                //如果有停售的菜品
                if (dishMapper.getById(dishId).getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
                //起售套餐
                setmeal.setStatus(StatusConstant.ENABLE);
            });
        } else {
            //否则停售套餐
            setmeal.setStatus(StatusConstant.DISABLE);
        }
        //更新数据
        setmealMapper.update(setmeal);
    }

}
